package com.zxl.demo;

import com.zxl.demo.model.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author zxl
 * @version 1.0
 * @date 2021/10/31 19:22
 */
public class SessionServlet extends HttpServlet {
    /**
     * session 一个浏览器只有唯一的session
     * 存在于服务器
     * 用户使用时返回的唯一的id
     */

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获得session
        HttpSession session = req.getSession();
        //设置session
        session.setAttribute("name",new User(1,"1",1));
        //获得session
        User user = (User) session.getAttribute("name");
        //判断是不是新的session
        boolean aNew = session.isNew();
        //注销session
        session.invalidate();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
